// pages/ordermsg/ordermsg.js
import Toast from '@vant/weapp/toast/toast'
import Dialog  from '@vant/weapp/dialog/dialog'

const $api = require('./../../utils/request').API
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataArray: [],
    selectedAll: false
  },

  onChange(e) {
    const arr = this.data.dataArray
    arr[e.target.id].checked = e.detail
    if ( arr.find( i => i.checked === false || i.checked == undefined) ) {
      this.setData({
        selectedAll: false
      })
    } else {
      this.setData({
        selectedAll: true
      })
    }
    this.setData({
      dataArray: arr
    })
  },

  changeAll() {
    const arr = this.data.dataArray
    if ( arr.find( i => i.checked === false || i.checked == undefined) ) {
      arr.map( i => i.checked = true)
      this.setData({
        dataArray: arr,
        selectedAll: true
      })
      return
    }

    arr.map( i => i.checked = false)
    this.setData({
      dataArray: arr,
      selectedAll: false
    })
  },

  deleteSelected () {
    const arr = this.data.dataArray
    if ( arr.find( i => i.checked === false || i.checked == undefined) ) {
      return Toast.fail('请至少选择一条数据进行删除');
    }
    
    Dialog.confirm({
      title: '是否确认删除所选消息',
    }).then(() => {
      // on confirm
      const arr = this.data.dataArray
      const resArr = []
      arr.map( i => {
        if ( i.checked === true ) {
          resArr.push(i.id)
        }
      })
      console.log(resArr)
    }).catch(() => {
        // on cancel
    });
  },

  getData() {
    const params = {
      pageNum: 1,
      pageSize: 999,
    }
    $api.getYuyueMsg(params).then( res => {
      this.setData({
        dataArray: res.data.list
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})